﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra.Double;
using System.Windows.Forms;

namespace WHW_NetUtility
{
    class MathNetHelper
    {
 //       double[,] my = new double[2, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };

        /// <summary>
        /// 求解多元一次方程组
        /// </summary>
        /// <param name="input">多元方程组的系数数组</param>
        /// <param name="vertor">多元方程组的值数组，即等号右边的值</param>
        /// <returns>返回求解的方程的数组</returns>
        public double[] SolveEquation(double[,] input,double[] vertor)
        {
            try
            {
                var matrixA = DenseMatrix.OfArray(input);   //用二维数组生成稠密矩阵
                var vertorA = new DenseVector(vertor);      //用一维数组生成稠密相量
                var Result = matrixA.LU().Solve(vertorA);   //用LU方法求解矩阵
                double[] resultD = Result.ToArray();        //将Vertor<T>转成相应的数组
                return resultD;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }            
        }

        /// <summary>
        /// 根据转换前后坐标系的两个点来确定坐标系平移坐标和旋转角度
        /// </summary>
        /// <param name="input">数组按x1,y1,x2,y2规则，x1,y1,x2,y2为转换后的坐标系两个坐标点</param>
        /// <param name="vertor">数组按xo1,yo1,xo2,yo2规则，xo1,yo1,xo2,yo2为转换前的坐标系的两个坐标点</param>
        /// <returns></returns>
        public double[] CoordinateTransform(double[] input, double[] vertor)
        {
            try
            {
                //输入的input数组按x1,y1,x2,y2规则，x1,y1,x2,y2为转换后的坐标系两个坐标点
                //输入的vertor数组按xo1,yo1,xo2,yo2规则，xo1,yo1,xo2,yo2为转换前的坐标系的两个坐标点
                double Xnew1 = input[0];
                double Ynew1 = input[1];
                double Xnew2 = input[2];
                double Ynew2 = input[3];
                double[,] matrixXY = new double[4, 4] { { Xnew1,Ynew1,1,0}, { Ynew1,-Xnew1,0,1}, { Xnew2, Ynew2, 1, 0 }, { Ynew2, -Xnew2, 0, 1 } };
                var matrixA = DenseMatrix.OfArray(matrixXY);   //用二维数组生成稠密矩阵
                var vertorA = new DenseVector(vertor);      //用一维数组生成稠密相量
                var Result = matrixA.LU().Solve(vertorA);   //用LU方法求解矩阵
                double[] resultD = Result.ToArray();        //将Vertor<T>转成相应的数组
                return resultD;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

    }
}
